package com.example.zhinongbao.activity;



import android.os.Bundle;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.example.zhinongbao.R;
import com.example.zhinongbao.model.User;
import com.example.zhinongbao.presenter.PersonalInfoPresenter;

public class PersonalInfoActivity extends AppCompatActivity
        implements PersonalInfoPresenter.View {

    private EditText etUsername, etEmail, etPhone;
    private ImageView ivAvatar;
    private Button btnSave;
    private PersonalInfoPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);

        // 初始化视图
        ivAvatar = findViewById(R.id.iv_avatar);
        etUsername = findViewById(R.id.et_username);
        etEmail = findViewById(R.id.et_email);
        etPhone = findViewById(R.id.et_phone);
        btnSave = findViewById(R.id.btn_save);

        // 初始化Presenter
        presenter = new PersonalInfoPresenter(this);

        // 加载用户数据
        presenter.loadUserData();

        // 保存按钮点击事件
        btnSave.setOnClickListener(v -> saveUserInfo());
    }

    private void saveUserInfo() {
        User user = new User();
        user.setUsername(etUsername.getText().toString().trim());
        user.setEmail(etEmail.getText().toString().trim());
        user.setPhone(etPhone.getText().toString().trim());

        presenter.saveUserInfo(user);
    }

    @Override
    public void displayUserInfo(User user) {
        // 显示用户信息
        Glide.with(this).load(user.getAvatar()).into(ivAvatar);
        etUsername.setText(user.getUsername());
        etEmail.setText(user.getEmail());
        etPhone.setText(user.getPhone());

    }

    @Override
    public void showSaveSuccess() {
        Toast.makeText(this, "个人信息保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, "保存失败: " + message, Toast.LENGTH_SHORT).show();
    }
}
